package cn.itsource;

import cn.itsource.domain.Student;

import com.alibaba.fastjson.JSONArray;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
public class RedisServiceTest {

    @Autowired
    private RedisService redisService;

    @Test
    public void testSave(){
        /**
         * 在实际应用场景中 存数据主要有三种类型
         * 1,存字符串
         * 2 存对象
         * 3 存集合
         */
        //1 保存字符串
        redisService.setStringKeyAndValue("student","我是学生");

        //2 保存对象
        Student stu= new Student();
        stu.setName("张三");
        stu.setAge(21);
        stu.setAddress("洪山区");
        redisService.setStringKeyAndValue("stuobj","stu");
        
        //3保存集合---转成json格式
        ArrayList<Student> stuList = new ArrayList<>();
        stuList.add(new Student("李四",20,"青山区"));
        stuList.add(new Student("早四",20,"青山区"));
        stuList.add(new Student("王四",20,"青山区"));
        redisService.setStringKeyAndValue("stuList", JSONArray.toJSONString(stuList));
    }

    @Test
    public void getData(){
        //取字符串
        String student = redisService.getKeyObjectValue("student", String.class);
        System.out.println("String类型...."+student);

        //取对象
        Student stuobj = redisService.getKeyObjectValue("stuobj", Student.class);
        System.out.println("Object类型+++"+stuobj);
        //取集合
        String jsonStuList = redisService.getKeyObjectValue("stuList", String.class);
        //转换格式
        List<Student> stuList=JSONArray.parseArray(jsonStuList,Student.class);

        for (Student student1:stuList) {
            System.out.println(student1.toString());

        }

    }

}
